/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.fragment.BaseMaterialFragment;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.ScrollView;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import ohos.hiviewdfx.HiLog;

import java.io.IOException;

import static ohos.multimodalinput.event.TouchEvent.*;

/**
 * Card Shadow Examples
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeShadowFragment extends BaseMaterialFragment {

    protected ScrollView mScrollView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_shadow);
        mScrollView = (ScrollView) this.findComponentById(ResourceTable.Id_card_scrollview);

        initCards();
    }

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_cardshadowsubtitle;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group1;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    private void initCards() {
        init_card_elevation_shadow();
        init_card_animation_shadow();
    }

    /**
     * This method builds a card with a different elevation
     */
    private void init_card_elevation_shadow() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
                     HiLog.error(Constants.LABEL_LOG,e.toString());
        }

        card.addCardHeader(header);

        //Card elevation
        try {
            card.setCardElevation(getResourceManager().getElement(ResourceTable.Float_carddemo_shadow_elevation).getFloat());
        } catch (Exception e) {
             HiLog.error(Constants.LABEL_LOG,e.toString());
        }

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_shadow_elevation);
        cardView.setCard(card);
    }

    /**
     * This method builds a card with an animation
     */
    private void init_card_animation_shadow() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
                     HiLog.error(Constants.LABEL_LOG,e.toString());
        }

        card.addCardHeader(header);

        //Set card in the cardView
        final CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_shadow_animation);
        cardView.setCard(card);

        cardView.setOnTouchListener((component, touchEvent) -> {
            switch (touchEvent.getAction()) {
                case PRIMARY_POINT_DOWN:
                    AnimatorProperty animatorProperty = cardView.createAnimatorProperty();
                    animatorProperty.scaleX(1.1f).scaleY(1.1f).setDuration(100);
                    animatorProperty.start();
                    return true;
                case CANCEL:
                case PRIMARY_POINT_UP:
                    AnimatorProperty animatorProperty1 = cardView.createAnimatorProperty();
                    animatorProperty1.scaleX(1).scaleY(1).setDuration(100);
                    animatorProperty1.start();
                    return true;
            }
            return false;
        });
    }

}
